﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CComponentData))]
    public class CComponent
    {
        #region fields

        protected CGameObject m_gameObject;

        #endregion

        #region properties

        [ContentSerializerIgnore]
        public virtual CGameObject GameObject
        {
            get { return m_gameObject; }
            set
            {
                if (m_gameObject == value)
                    return;
                if (m_gameObject != null)
                {
                    m_gameObject.OnUpdate -= Update;
                    m_gameObject.OnInitialize -= Initialize;
                    m_gameObject.RemoveComponent(this);
                }
                m_gameObject = value;
                m_gameObject.AddComponent(this);
                m_gameObject.OnUpdate += Update;
                m_gameObject.OnInitialize += Initialize;
                if (m_gameObject.IsInitialized)
                    Initialize(m_gameObject);
            }
        }

        #endregion

        #region ctor

        #endregion

        #region methods

        public CComponent Clone()
        {
            return Clone(m_gameObject);
        }

        public virtual CComponent Clone(CGameObject gameObject)
        {
            CComponent component = new CComponent();
            component.GameObject = gameObject;
            return component;
        }

        public virtual void Initialize(CGameObject gameObject)
        {
        }

        public virtual void Update(CGameObject gameObject)
        {
        }


        #endregion
    }
}
